package com.laze.mywanandroid.ui.main.fragment.home


import android.app.ActivityManager
import android.content.Intent
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.loadmore.LoadMoreStatus
import com.hjq.toast.ToastUtils
import com.laze.mywanandroid.R
import com.laze.mywanandroid.base.BaseVmFragment
import com.laze.mywanandroid.bean.ArticleData
import com.laze.mywanandroid.bean.Banner
import com.laze.mywanandroid.ui.details.DetailsActivity
import com.laze.mywanandroid.utils.core.ActivityUtil
import com.youth.banner.BannerConfig
import kotlinx.android.synthetic.main.home_fragment.*

class HomeFragment : BaseVmFragment<HomeViewModel>() {

    private lateinit var mAdapter: ArticleAdapter

    companion object {
        fun newInstance() = HomeFragment()
    }

    override fun layoutRes(): Int = R.layout.home_fragment

    override fun viewModelClass(): Class<HomeViewModel> = HomeViewModel::class.java

    override fun initView() {
        super.initView()
        mAdapter = ArticleAdapter(R.layout.item_article).apply {
            loadMoreModule.setOnLoadMoreListener {
                viewModel.loadMore()
            }
            setOnItemClickListener { _, _, position ->
                val article = mAdapter.data[position]
                ActivityUtil.start(
                    DetailsActivity::class.java,
                    mapOf(DetailsActivity.PARAM_ARTICLE to article)
                )
            }
        }
        rvArticle.run {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    override fun observe() {
        super.observe()
        viewModel.banner.observe(viewLifecycleOwner, Observer {
            setBanner(it)
        })
        viewModel.articleList.observe(viewLifecycleOwner, Observer {
            mAdapter.setList(it)
        })
        viewModel.loadMoreStatus.observe(viewLifecycleOwner, Observer {
            when (it) {
                LoadMoreStatus.Loading -> mAdapter.loadMoreModule.loadMoreToLoading()
                LoadMoreStatus.Complete -> mAdapter.loadMoreModule.loadMoreComplete()
                LoadMoreStatus.Fail -> mAdapter.loadMoreModule.loadMoreFail()
                LoadMoreStatus.End -> mAdapter.loadMoreModule.loadMoreEnd()
                else -> return@Observer
            }
        })
    }

    private fun setBanner(banners: List<Banner>) {
        bannerView.apply {
            setBannerStyle(BannerConfig.NOT_INDICATOR)
            setImageLoader(BannerImageLoader())
            setImages(banners)
            setDelayTime(2000)
            start()
            setOnBannerListener {
                val banner = banners[it]
                ActivityUtil.start(
                    DetailsActivity::class.java,
                    mapOf(
                        DetailsActivity.PARAM_ARTICLE to ArticleData(
                            title = banner.title,
                            link = banner.url
                        )
                    )
                )
            }
        }
    }

    override fun initData() {
        super.initData()
        viewModel.getData()
    }

    override fun onStart() {
        super.onStart()
        bannerView.startAutoPlay()
    }

    override fun onStop() {
        super.onStop()
        bannerView.stopAutoPlay()
    }

}